#include "DoubleLinkedListWithHead.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
	Create_Empty_DoubleLinkedListWithHead:创建一条空的双向链表
*/
struct Head*Create_Empty_DoubleLinkedListWithHead()
{
    struct Head*h = malloc(sizeof(struct Head));
    if(h == NULL)
        return h;
    h->num = 0;
    h->first = h->last = NULL;

    return h;
}

/*
	Print_DoubleLinkedListWithHead:输出一条带头结点的整型双向链表
	@h:要输出的链表的头节点指针
	
	返回值：
		无
*/
void Print_DoubleLinkedListWithHead(struct Head*h)
{
    if(h == NULL)
    {
        return ;
    }
    printf("=========================\n");
    if(h->first == NULL && h->last == NULL && h->num == 0)//空链表
    {
        printf("num:%d\n", h->num);
    }
    else
    {
        printf("first->data:%s last->data:%s num:%d\n", h->first->data,h->last->data,h->num);
    }

    printf("Next:");
    struct Node*p = h->first;
    while(p)
    {
        printf("%s\n", p->data);
        p = p->next;
    }


    printf("Pre:");
    p = h->last;
    while(p)
    {
        printf("%s\n", p->data);
        p = p->pre;
    }
   
}
/*
	Insert_node_y：在h指向的链表中，增加一个数据域为x的节点
*/
void Insert_node_y(struct Head*h, ElemType x)
{
    //1.创建新节点并赋值
    struct Node*pnew = malloc(sizeof(*pnew));
    if(pnew == NULL)
        return ;
    pnew->data = malloc(strlen(x)+1);
    strcpy(pnew->data, x);
    pnew->next = pnew->pre = NULL;
    if(h->first == NULL && h->last == NULL && h->num == 0)//空链表
    {
        h->first = h->last = pnew;
        h->num++;
        return;
    }
    else
    {
        pnew->pre = h->last;
        h->last->next = pnew;
        h->last = pnew;
    }
    h->num++;

}
